package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Teacher;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.StudentService;
import com.zhiyun.service.TeacherService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.List;
import java.util.Scanner;

public class MyCourseView {
    TeacherService teacherService=CilentProxy.getProxyInstance(TeacherService.class);
    StudentService studentService=CilentProxy.getProxyInstance(StudentService.class);
    CourseService courseService= CilentProxy.getProxyInstance(CourseService.class);
   Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~~~我的课程界面~~~~~~~~~~~");
            System.out.println("1.课程列表");
            System.out.println("2.添加课程");
            System.out.println("3.进入课程");
            System.out.println("4.退出课程");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allCourse();
                    break;
                case 2:
                    inseretCourse();
                    break;
                case 3:
                    enterCourse();

                    break;
                case 4:
                    delCourse();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void enterCourse() {
       while (true){
           Result result = courseService.stuCourseList(AppData.student.getUserId());
           if (result.getCode()==200){
               List<Course> obj = (List<Course>) result.getObj();
               for (Course course : obj) {
                   Result result1 = teacherService.myInfo(course.getTeacherId());
                   if (result1.getCode()==200){
                       Teacher obj1 = (Teacher) result1.getObj();
                       System.out.println(course.getId()+"   课程名称:"+course.getName()+
                               (obj1.getTruename()==null?" ":("    老师姓名:"+obj1.getNickname()))+"   老师id:"+course.getTeacherId());
                   }
               }
           }else {
               System.out.println("没有课程,无法进入");
               break;
           }
           System.out.println("请输入想要进入的课程id");
           int id = scanner.nextInt();
           if (result.getCode()==200){
               List<Course> obj = (List<Course>) result.getObj();
               for (Course course : obj) {
                  if (id==course.getId()){
                      new MyCourseView_In().start(course);
                      return;
                  }
               }
           }

       }
    }
/*    ~~~~~~  写了没写完  ~~~~~~~      */
    private void delCourse() {
        while (true){
            boolean a=false;
            Result result = courseService.stuCourseList(AppData.student.getUserId());
            if (result.getCode()==200){
                List<Course> obj = (List<Course>) result.getObj();
                for (Course course : obj) {
                    System.out.println(course);
                }
                System.out.println("请输入想要退出的课程id");
                int cid = scanner.nextInt();
                for (Course course : obj) {
                    if (course.getId()==cid){
                        a=true;
                    }
                }
                if (a){
                    Result result1 = courseService.exitCourse(AppData.student.getId(), cid);
                    if (result1.getCode()==200){
                        System.out.println("退出课程成功");
                    }else {
                        System.out.println("退出课程失败~~~");
                    }
                    break;
                }else {
                    System.out.println("想要退出的课程不存在,请重新输入");
                    continue;
                }
            }else {
                break;
            }
        }
    }

    private void inseretCourse() {
        while (true){
            Result result = courseService.stuCourseList(AppData.student.getUserId());
            if (result.getCode()==200){
                List<Course> obj = (List<Course>) result.getObj();
                for (Course course : obj) {
                    System.out.println("课程id"+course.getId()+"    课程名称"+course.getName());
                }
            }else {

            }
            System.out.println("请输入邀请码");
            String next = scanner.next();

            Result result1 = courseService.addCourse(AppData.student.getId(),next);
            if (result1.getCode()==200){
                System.out.println("添加课程成功");
                break;
            }else {
                System.out.println("添加课程失败,课程号错误或邀请码错误,请重新输入");
                break;
            }
        }
    }

    private void allCourse() {
       while (true){
           Result result = courseService.stuCourseList(AppData.student.getId());
           if (result.getCode()==200){
               List<Course> obj = (List<Course>) result.getObj();
               for (Course course : obj) {
                   System.out.println("课程id"+course.getId()+"    课程名称"+course.getName());
               }
           }else {

           }
           break;
       }
    }
}
